package com.jservice.web.smkRefund;

import com.jservice.framework.dto.Order;
import com.jservice.framework.dto.Pageable;
import com.jservice.framework.dto.Pager;
import com.jservice.framework.spring.SpringContextUtil;
import com.jservice.model.SmkRefunds;
import com.jservice.web.base.JBaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * SmkReFundService - 退款业务
 *
 * @Author cwledit
 * @DATE 2017/6/21.
 */
@Service("smkReFundService")
@Transactional(readOnly = true)
public class SmkReFundService extends JBaseService{

    public static final SmkRefunds DAO = new SmkRefunds();

    public static SmkReFundService me() {
        return SpringContextUtil.getBean("smkReFundService");
    }

    public Pager findPage(Pageable pageable) {
        Order order1 = new Order();
        order1.setProperty("create_date");
        order1.setDirection(Order.Direction.desc);

        pageable.getOrders().add(order1);
        return findPage(pageable,DAO, SmkRefunds.TABLE_NAME);
    }

    /**
     * @param out_trade_no 商户退款单号
     * @param trade_status  3	退款处理中	处理中
                            4	退款成功	支付宝或微信异步通知退款成功
                            5	退款失败	支付宝或微信异步通知退款失败
     * @param refund_trade_no       退款单号
     * @param refund_reason         退款原因
     * @return
     */
    @Transactional(readOnly = false)
    public boolean updateStatus(String out_trade_no, String trade_status, String refund_trade_no, String refund_reason){
        SmkRefunds smkRefunds = SmkRefunds.dao.doFindFirst(" sn = ? and refund_trade_no = ?",out_trade_no,refund_trade_no);
        if (smkRefunds == null){
            return false;
        }
        smkRefunds.setStatus(trade_status);
        smkRefunds.setModifyDate(new Date());
        smkRefunds.setRefundReason(refund_reason);
        return smkRefunds.update();
    }

}
